package com.taijihuabao.common.exception;

import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.common.jsonresult.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author Luo
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        JsonResult result;
        if (e instanceof ServiceException) {
            result = JsonResult.failure(((ServiceException) e).getResultCode());
        } else {
            result = JsonResult.failure(ApplicationResponseCode.ERROR, "服务器错误" + e.getMessage());
        }
        return result;
    }

}
